<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属工厂" prop="factoryId">
        <el-select
          v-model="queryParams.factoryId"
          placeholder="请选择所属工厂"
          clearable
          size="small"
        >
          <el-option
            v-for="dept in deptOptions"
            :key="dept.deptId"
            :label="dept.name"
            :value="dept.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计划编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入计划编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入计划名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="养护级别" prop="level">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择养护级别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="计划状态" prop="status">-->
      <!--        <el-select v-model="queryParams.status" placeholder="请选择计划状态" clearable size="small">-->
      <!--          <el-option label="请选择字典生成" value="" />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="养护周期" prop="cycle">
        <el-select
          v-model="queryParams.cycle"
          placeholder="请选择养护周期"
          clearable
          size="small"
          filterable
        >
          <el-option
            v-for="dict in cycleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="责任人" prop="dutyPerson">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.dutyPerson"-->
      <!--          placeholder="请输入责任人"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:maintainPlan:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:maintainPlan:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:maintainPlan:remove']"
          >删除</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['equipment:maintainPlan:export']"-->
      <!--          >导出</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="maintainPlanList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="计划编号" align="center" prop="code" />
      <el-table-column label="计划名称" align="center" prop="name" />
      <el-table-column
        label="养护级别"
        align="center"
        prop="level"
        :formatter="levelFormat"
      />
      <el-table-column
        label="养护周期"
        align="center"
        prop="cycle"
        :formatter="cycleFormat"
      />
      <el-table-column label="责任人" align="center" prop="dutyPersonName" />
      <el-table-column label="所属工厂" align="center" prop="factoryName" />
      <el-table-column
        label="是否启用"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:maintainPlan:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:maintainPlan:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改养护计划对话框 -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入计划编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入计划名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护级别" prop="level">
              <el-select
                v-model="form.level"
                placeholder="请选择养护级别"
                :disabled="openDisabled"
              >
                <el-option
                  v-for="dict in levelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="提醒时间" prop="warnTime">-->
          <!--              <el-date-picker :editable="false" clearable size="small"-->
          <!--                              v-model="form.warnTime"-->
          <!--                              type="date"-->
          <!--                              value-format="yyyy-MM-dd"-->
          <!--                              placeholder="选择提醒时间" :disabled="openDisabled">-->
          <!--              </el-date-picker>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="提醒内容" prop="warnContent">-->
          <!--              <el-input v-model="form.warnContent" placeholder="请输入提醒内容" :disabled="openDisabled" />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="计划状态">-->
          <!--              <el-select v-model="form.status" placeholder="请选择计划状态" :disabled="openDisabled">-->
          <!--                <el-option-->
          <!--                  v-for="dict in statusOptions"-->
          <!--                  :key="dict.dictValue"-->
          <!--                  :label="dict.dictLabel"-->
          <!--                  :value="dict.dictValue"-->
          <!--                ></el-option>-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="养护周期" prop="cycle">
              <el-select
                v-model="form.cycle"
                placeholder="请选择养护周期"
                :disabled="openDisabled"
              >
                <el-option
                  v-for="dict in cycleOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="养护类型" prop="maintainType">-->
          <!--              <el-select v-model="form.maintainType" placeholder="请选择养护类型" :disabled="openDisabled">-->
          <!--                <el-option label="请选择字典生成" value="" />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="责任人" prop="dutyPerson">
              <UserDialog
                @dialogChange="form.dutyPerson = $event.value"
                :echoValue="form.dutyPersonName"
                :multiple="false"
                :openDisabled="openDisabled"
                placeholder="请选择责任人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属工厂" prop="factoryId">
              <el-select
                v-model="form.factoryId"
                placeholder="请选择所属工厂"
                clearable
                size="small"
                :disabled="openDisabled"
              >
                <el-option
                  v-for="dept in deptOptions"
                  :key="dept.deptId"
                  :label="dept.name"
                  :value="dept.deptId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="养护设备" prop="maintainDevice">
                <!--              <el-input v-model="form.maintainDevice" placeholder="请输入养护设备" :disabled="openDisabled" />-->
                <Dialog
                  @dialogChange="form.maintainDevice = $event.value"
                  :echoValue="form.deviceName"
                  parameter="rows"
                  value="id"
                  label="name"
                  search="code/name"
                  url="/basic/eqpInfo/list"
                  :column="['设备编号/code', '设备名称/name']"
                  :openDisabled="openDisabled"
                  placeholder="请选择内容"
                  :axiosData="{ deptId: 101 }"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否启用">
                <el-radio-group v-model="form.status" :disabled="openDisabled">
                  <el-radio
                    v-for="dict in statusOptions"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                    >{{ dict.dictLabel }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.createTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                format="yyyy-MM-dd HH:mm"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">养护计划内容信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddEqpMaintainPlanContent"
              :disabled="openDisabled"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteEqpMaintainPlanContent"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="eqpMaintainPlanContentList"
          :row-class-name="rowEqpMaintainPlanContentIndex"
          @selection-change="handleEqpMaintainPlanContentSelectionChange"
          ref="eqpMaintainPlanContent"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="项目" prop="itemName">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.itemName"
                placeholder="请输入项目"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="保养内容" prop="maintainContent">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.maintainContent"
                placeholder="请输入保养内容"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <!--          <el-table-column label="备注" prop="remark">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaintainPlan,
  getMaintainPlan,
  delMaintainPlan,
  addMaintainPlan,
  updateMaintainPlan,
} from "@/api/equipment/maintain/plan";
import { getFactoryList } from "@/api/equipment/common";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "MaintainPlan",
  components: { Treeselect },
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEqpMaintainPlanContent: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 养护计划表格数据
      maintainPlanList: [],
      // 养护计划内容表格数据
      eqpMaintainPlanContentList: [],
      // 项目字典
      itemNameOptions: [],
      // 保养内容字典
      maintainContentOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 养护级别字典
      levelOptions: [],
      // 养护周期字典
      cycleOptions: [],
      //计划状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        code: null,
        name: null,
        level: null,
        status: null,
        cycle: null,
        dutyPerson: null,
        factoryId: null,
      },
      // 部门树选项
      deptOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [{ required: true, message: "不能为空", trigger: "blur" }],
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        cycle: [{ required: true, message: "不能为空", trigger: "blur" }],
        factoryId: [{ required: true, message: "不能为空", trigger: "blur" }],
        maintainDevice: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("maintain_level").then((response) => {
      this.levelOptions = response.data;
    });
    this.getDicts("maintain_cycle").then((response) => {
      this.cycleOptions = response.data;
    });
    this.getDicts("sys_show_hide").then((response) => {
      this.itemNameOptions = response.data;
    });
    this.getDicts("sys_job_status").then((response) => {
      this.maintainContentOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.statusOptions = response.data;
    });
    // this.getTreeselect();
    this.getFactory();
  },
  methods: {
    getFactory() {
      getFactoryList().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // /** 查询部门下拉树结构 */
    // getTreeselect() {
    //     treeselect().then((response) => {
    //         this.deptOptions = response.data;
    //     });
    // },
    /** 查询养护计划列表 */
    getList() {
      this.loading = true;
      listMaintainPlan(this.queryParams).then((response) => {
        this.maintainPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 养护级别字典翻译
    levelFormat(row, column) {
      return this.selectDictLabel(this.levelOptions, row.level);
    },
    // 养护周期字典翻译
    cycleFormat(row, column) {
      return this.selectDictLabel(this.cycleOptions, row.cycle);
    },
    // 养护周期字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        level: null,
        warnTime: null,
        warnContent: null,
        status: "Y",
        cycle: null,
        maintainType: null,
        dutyPerson: null,
        factoryId: null,
        maintainDevice: null,
        startTime: null,
        endTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        deviceName: null,
        factoryName: null,
        dutyPersonName: null,
      };
      this.eqpMaintainPlanContentList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.openDisabled = false;
      this.open = true;
      this.title = "添加养护计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getMaintainPlan(id).then((response) => {
        this.form = response.data;
        this.eqpMaintainPlanContentList =
          response.data.eqpMaintainPlanContentList;
        this.open = true;
        this.title = "修改养护计划";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getMaintainPlan(id).then((response) => {
        this.form = response.data;
        this.eqpMaintainPlanContentList =
          response.data.eqpMaintainPlanContentList;
        this.open = true;
        this.title = "查看养护计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.eqpMaintainPlanContentList =
            this.eqpMaintainPlanContentList;
          if (this.form.id != null) {
            updateMaintainPlan(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addMaintainPlan(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      // this.$confirm('是否确认删除养护计划编号为"' + ids + '"的数据项?', "警告", {
      this.$confirm("是否确认删除选中的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delMaintainPlan(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 养护计划内容序号 */
    rowEqpMaintainPlanContentIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 养护计划内容添加按钮操作 */
    handleAddEqpMaintainPlanContent() {
      let obj = {};
      obj.itemName = "";
      obj.maintainContent = "";
      obj.remark = "";
      this.eqpMaintainPlanContentList.push(obj);
    },
    /** 养护计划内容删除按钮操作 */
    handleDeleteEqpMaintainPlanContent() {
      if (this.checkedEqpMaintainPlanContent.length == 0) {
        this.$alert("请先选择要删除的养护计划内容数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.eqpMaintainPlanContentList.splice(
          this.checkedEqpMaintainPlanContent[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleEqpMaintainPlanContentSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.eqpMaintainPlanContent.clearSelection();
        this.$refs.eqpMaintainPlanContent.toggleRowSelection(selection.pop());
      } else {
        this.checkedEqpMaintainPlanContent = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equipment/maintainPlan/export",
        {
          ...this.queryParams,
        },
        "养护计划" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
